package com.octopus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

/**
 * @Author: Coder-zcx
 * @Date: 2025/01/10 17:23
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        Date startTime = new Date();
        ConfigurableApplicationContext context = new SpringApplicationBuilder(Application.class)
                .run(args);
        printStartupPrompt(startTime, context.getEnvironment());
    }

    public static void printStartupPrompt(Date startTime, Environment env) {
        String port = env.getProperty("server.port");
        String duration = toDuration(System.currentTimeMillis() - startTime.getTime(), true);
        System.out.printf("Server startup on port %S, elapsed time %S", port, duration);
    }

    /** 转换为持续时间字符串 **/
    public static String toDuration(long time, boolean useMillisecond) {
        long day = 24 * 60 * 60 * 1000;
        String pattern = useMillisecond ? "HH:mm:ss.SSS" : "HH:mm:ss";
        SimpleDateFormat fmt = new SimpleDateFormat(pattern, Locale.US);
        fmt.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        String string = fmt.format(new Date(time));
        if (time >= day) {
            string = (time / day) + "天" + string;
        }
        return string;
    }

}
